// 绑定保存按钮事件
function bindEventForSaveBtn($form, $taskMaxNumber, $batchNumber, $threadNumber, $wsApprovalIPs){
	$("#systemConfigSubBtn").click(function() {
		if($form.formValidate()==false){
			return;
		}
		
		// 点击确定确认登录请求后台
		var data = {
			"fastsyncer.worker.task.max.number" : $taskMaxNumber.val(),
			"fastsyncer.worker.batch.number" : $batchNumber.val(),
			"fastsyncer.worker.thread.number" : $threadNumber.val(),
			"fastsyncer.rest.ws.approval.ips" : $wsApprovalIPs.val()
		};
		
		$.post($basePath + "/system/update", {
			"params" : JSON.stringify(data)
		}, function(data) {
			if (data.success == true) {
				bootGrowl(data.resultValue, "success");
			} else {
				// 请求失败
				bootGrowl(data.resultValue,"danger");
			}
		});
	});
};

// 初始化系统参数
function initSysConfigParams($taskMaxNumber, $batchNumber, $threadNumber, $wsApprovalIPs) {
	var params = JSON.parse($("#system_params").html());
	var taskMaxNumber = params[SystemParamsTaskMaxNumber];
	var batchNumber = params[SystemParamsBatchNumber];
	var threadNumber = params[SystemParamsThreadNumber];
	var wsApprovalIPs = params[SystemParamsWebserviceApprovalIPs];
	
	$taskMaxNumber.val(taskMaxNumber);
	$batchNumber.val(batchNumber);
	$threadNumber.val(threadNumber);
	$wsApprovalIPs.val(wsApprovalIPs);
};

$(function() {
	var $form = $("#systemConfigForm");
	var $taskMaxNumber = $form.find('input[name=\"'+SystemParamsTaskMaxNumber+'\"]');
	var $batchNumber = $form.find('input[name=\"'+SystemParamsBatchNumber+'\"]');
	var $threadNumber = $form.find('input[name=\"'+SystemParamsThreadNumber+'\"]');
	var $wsApprovalIPs = $form.find('textarea[name=\"'+SystemParamsWebserviceApprovalIPs+'\"]');

	// 初始化系统参数
	initSysConfigParams($taskMaxNumber, $batchNumber, $threadNumber, $wsApprovalIPs);

	// 绑定保存按钮事件
	bindEventForSaveBtn($form, $taskMaxNumber, $batchNumber, $threadNumber, $wsApprovalIPs);
	
	// 兼容IE PlaceHolder
	$('input[type="text"],input[type="password"],textarea').ASDPlaceHolder();
});

